package cha;

import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * This class will have the robot's gun follow the enemy
 * and shoot at the enemy.
 * 
 * @author Hansen Cheng
 *
 */
public class Destroyer extends Robot {
  
  /**
   * Robot starts to operate.
   */
  public void run() {
    
    while (true) {
      turnRadarRight(30);
    }
  }
  
  /**
   * Scans for a presence of a robot and fires at the robot.
   * 
   * @param e  - to catch the action of the robot.
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    //Name of enemy robot
    String enemyName = "";
    
    // If the robot has not following an enemy, get the name of 
    //the enemy once radar contact is made.
    if (("").equals(enemyName)) {
      enemyName = e.getName();
    }

    ///Tracks a robot but the name isn't the name of the previous enemy.
    if (!(e.getName().equals(enemyName))) {
      enemyName = "";
    }
    if (e.getName().equals(enemyName)) {
        turnRight(e.getBearing());
        ahead(100);
        fire(100);
        scan();
    }
  }
  
  /**
   * Hits the enemy robot and increase fire power.
   * 
   * @param e - to get the action of the robot.
   */
  public void onHitRobot(HitRobotEvent e) {
    fire(200);
  }
  
  /**
   * Robot is hit by enemy and it fires back with
   * bullet power of 100.
   * 
   * @param e - to catch the action of the robot.
   */
  public void onHitByBullet(HitByBulletEvent e) {
    setBodyColor(Color.RED);
    fire(200); 
  }
}
